//
//  NetVC.swift
//  XiaohuijiVPN
//
//  Created by whale on 2022/10/20.
//

import UIKit
import YYText
import Lottie
class NetVC: ICBaseVC {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        netAnimV.play()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        netAnimV.pause()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       _handleEvent()
    }
    
    override func _initData() {
        list.forEach { item in
            let itemV = NetItemView()
            itemV.topLbl.text = item.0
            itemV.bottomLbl.text = item.1
            itemV.bottomLbl.maxLine = 10
            bgSv.addSubview(itemV)
            itemViews.append(itemV)
        }
    }
    
    override func _initUI() {
        super._initUI()
        navBar.titleV.text = "首页"
        view.addSubview(bgSv)
        bgSv.da.addSubviews(sub: [netLbl, netAnimV])
    }
    
    override func _handleEvent() {
        ICNetworkState.shared.listen { state in
            switch state {
            case .wifi:
                self.netLbl.textColor = ._green
                self.netLbl.text = "您已连接WIFI"
                self.setupAnim(name: "wifi")
            case .mobile:
                self.netLbl.textColor = ._green
                self.netLbl.text = "您当前正在使用移动网络"
                self.setupAnim(name: "mobile")
            case .none:
                self.netLbl.textColor = .content
                self.netLbl.text = "网络走丢了"
                self.setupAnim(name: "no_net")
            }
        }
    }
    
    private func setupAnim(name: String) {
        let animation = Animation.named(name, subdirectory: "lottie")
        netAnimV.animation = animation
        netAnimV.play()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bgSv.frame = CGRect(x: 0, y: UIScreen.navH, width: view.bounds.width, height: view.bounds.height - UIScreen.navH - UIScreen.tabBarH)
        netLbl.left_right(bgSv, x: 12.adapter).height(40.adapter).top(0)
        netAnimV.left_right_offsetTop(netLbl).height(280.adapter)
        var lastY: CGFloat = netAnimV.frame.maxY
        itemViews.forEach({
            $0.left_right(netAnimV).top(lastY)
            $0.initFrame()
            lastY = $0.frame.maxY
        })
        bgSv.contentSize = CGSize(width: bgSv.bounds.width, height: lastY + 50.adapter)
    }
    
    private lazy var bgSv: UIScrollView = {
        let sv = UIScrollView()
        return sv
    }()
    
    private let netLbl = YYLabel()
    private lazy var netAnimV: AnimationView = {
        let animation = Animation.named("no_net", subdirectory: "lottie")
        let animV = AnimationView()
        animV.animation = animation
        animV.contentMode = .scaleAspectFit
        animV.loopMode = .loop
        animV.backgroundBehavior = .pauseAndRestore
        return animV
    }()
    
    private var itemViews: [NetItemView] = []
    
    private lazy var list: [(String, String)] = [
        ("一、什么是VPN", "VPN全称虚拟专用网络，属于远程访问技术，简单地说就是利用公用网络架设专用网络。例如某公司员工出差到外地，他想访问企业内网的服务器资源，这种访问就属于远程访问。"),
        ("二、VPN工作原理", "1、VPN网关采取双网卡结构，外网卡使用公网IP接入Internet。\n2、网络一的VPN网关将VPN数据包发送到Internet，网络二的VPN网关对接收到的数据包进行检查，确认无误后，两个网络内的终端就可以相互通讯了。"),
        ("三、怎么使用VPN", "点击vpn页面连接，会自动配置VPN代理，配置成功，即可使用"),
        ("四、订阅相关", "您的活期账户为每月特优套餐。如果您想取消/升级/降级，请访问您的订阅账户。"),
    ]
}

